//
//  YHMyMerchantDetailsViewController.m
//  YHApp
//
//  Created by -- on 2021/7/7.
//

#import "HAMerchantDetailsVC.h"
#import "HAMerchantDetailsMo.h"
#import "HAMachineRateVC.h"
#import "HAMachineServiceChargeVC.h"
#import "HAMerchantBindJiJuVC.h"

@interface HAMerchantDetailsVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HAMerchantDetailsMo *model;

@end

@implementation HAMerchantDetailsVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"商户详情";

    [self initUI];
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)initUI{

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(void)loadData {
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"customer/detail";
    request.parms = @{@"customer_id":self.customer_id?:@""};

    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAMerchantDetailsMo mj_objectWithKeyValues:dataModel.data];
                                
                [self.dataArray removeAllObjects];
                
                [self.dataArray addObject:@"商户信息"];
                [self.dataArray addObject:@"直属代理"];
                [self.dataArray addObject:@"商户名称"];
                [self.dataArray addObject:@"商户编号"];
                [self.dataArray addObject:@"绑定时间"];

                [self.dataArray addObject:@"机具概况"];
                [self.dataArray addObject:@"机具型号"];
                [self.dataArray addObject:@"机具编码"];
                [self.dataArray addObject:@"机具状态"];
                [self.dataArray addObject:@"绑定政策"];
                [self.dataArray addObject:@"交易费率"];
                [self.dataArray addObject:@"代收服务费"];
                [self.dataArray addObject:@"入库时间"];
                [self.dataArray addObject:@"首次交易"];
                
                [self.dataArray addObject:@"交易情况"];
                [self.dataArray addObject:@"本月交易额"];
                [self.dataArray addObject:@"累计交易额"];

                [self.tableView reloadData];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"商户信息"] || [title isEqualToString:@"交易情况"] || [title isEqualToString:@"机具概况"]) {
        return 45;
    }else {
        return 55;
    }
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMyMerchantDetailsCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMyMerchantDetailsCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"商户信息"] || [title isEqualToString:@"交易情况"] || [title isEqualToString:@"机具概况"]) {
        [self setCellTitWith:cell.contentView title:title];
    }else if ([title isEqualToString:@"直属代理"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.agents_name isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"商户名称"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.surname isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"商户编号"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.number isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"本月交易额"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.trade_month isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"累计交易额"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.trade_total isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"机具型号"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.termModel isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"机具编码"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.term_sn isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"交易费率"]) {
        [self setCellConWith:cell.contentView title:title content:@"查看" isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"代收服务费"]) {
        [self setCellConWith:cell.contentView title:title content:@"查看" isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"入库时间"]) {
        NSString *created_at = [NSString timeStampToString:[self.model.created_at integerValue] Formatter:@"yyyy-MM-dd HH:mm"];
        [self setCellConWith:cell.contentView title:title content:created_at isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"首次交易"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.firstTrade isLine:YES isRImg:NO];
    }else if ([title isEqualToString:@"绑定时间"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.bind_at isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"机具状态"]) {
        NSString *content = @"";
        if (self.model.is_bind == 1) {
            content = @"已绑定";
        }else {
            content = @"未绑定";
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES isRImg:YES];
    }else if ([title isEqualToString:@"绑定政策"]) {
        NSString *content = @"";
        if (self.model.policy && [[self.model.policy allKeys] containsObject:@"name"]) {
            content = [self.model.policy valueForKey:@"name"];
        }
        [self setCellConWith:cell.contentView title:title content:content isLine:YES isRImg:YES];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"交易费率"]) {
        HAMachineRateVC *vc = [[HAMachineRateVC alloc]init];
        vc.sn = self.model.term_sn;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([title isEqualToString:@"代收服务费"]) {
        HAMachineServiceChargeVC *vc = [[HAMachineServiceChargeVC alloc]init];
        vc.sn = self.model.term_sn;
        [self.navigationController pushViewController:vc animated:YES];
    }
    if ([title isEqualToString:@"机具编码"]) {
        if (![NSString isEmptyString:self.model.term_sn]) {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = self.model.term_sn;
            [MBProgressHUD showSuccess:@"已复制"];
        }
    }
    if ([title isEqualToString:@"机具状态"]) {
        HAMerchantBindJiJuVC *vc = [[HAMerchantBindJiJuVC alloc]init];
        vc.model = self.model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    

}

-(void)setCellTitWith:(UIView *)contentView title:(NSString *)title {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UILabel *label = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:title alignment:1];
    [contentView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(15);
    }];
}


-(void)setCellConWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline isRImg:(BOOL)isRImg {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor whiteColor];

    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];
    
    if (isRImg) {
        UIImageView *RImgV = [UIImageView ha_createImageView];
        RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [contentView addSubview:RImgV];
        [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
    }
    
    if ([NSString isEmptyString:content]) {
        content = @"--";
    }
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"858b9c" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        if (isRImg) {
            make.right.mas_equalTo(-10-20-10);
        }else {
            make.right.mas_equalTo(-20);
        }
    }];
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(1);
            make.bottom.mas_equalTo(0);
        }];
    }
}



- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}

@end
